<?php

class PerfilService extends Model {

    /**
     * @var PerfilDao
     */
    private $perfilDao;
    /**
     * @var FuncaoService
     */
    private $funcaoService;
    /**
     * @var UsuarioService
     */
    private $usuarioService;
    /**
     * @var AcaoService
     */
    private $acaoService;
    /**
     * @var MenuService
     */
    private $menuService;

    public function injectDependencies() {
        $this->perfilDao = PerfilDao::getInstance();
        $this->funcaoService = FuncaoService::getInstance();
        $this->usuarioService = UsuarioService::getInstance();
        $this->acaoService = AcaoService::getInstance();
        $this->menuService = MenuService::getInstance();
    }

    public function findAll() {
        $ArProfiles = $this->perfilDao->findAll();
        $this->fillProfile($ArProfiles);
        return $ArProfiles;
    }

    public function fillProfileWithPosition(&$MxProfile) {
        if(!is_array($MxProfile)) {
            $MxProfile->setFuncao($this->funcaoService->findSimpleById($MxProfile->getIDFuncao()));
            return;
        }
        foreach($MxProfile as &$Profile) {
            $Profile->setFuncao( $this->funcaoService->findSimpleById($Profile->getIDFuncao()) );
        }
    }

    public function save($ArDados) {
        return $this->perfilDao->save($ArDados);
    }

    //05-06
    public function findById($IDPerfil) {
        $Profile = $this->perfilDao->findById($IDPerfil);
        $this->fillProfile($Profile);
        return $Profile;
    }

    //06
    public function update($ArDados) {
        $ArDados['ArActions'] = $ArDados['ArActions'] ?: array();
        $this->acaoService->update($ArDados['ArActions'], $ArDados['IDPerfil']);
        if( !$this->perfilDao->update($ArDados) ) {
            return false;
        }
        if( $ArDados['IDPerfil'] == Dracosys::getLoggedUser()->getTPerfil()->getIDPerfil() ) {
            $this->usuarioService->reload();
            Dracosys::saveSessionValue(SESSION_SYSTEM, 'MainMenu', $this->menuService->buildMenuFromLoggedUser($ArDados['IDPerfil']));
        }
        return true;
    }

    public function findByPosition($IDFuncao) {
        return $this->perfilDao->findByPosition($IDFuncao);
    }

    public function changePosition($ArProfilesId) {
        return !empty($ArProfilesId) ? $this->perfilDao->changePosition($ArProfilesId) : true;
    }

    public function delete($IDPerfil, $ArChangesUsers) {
        if( !empty($ArChangesUsers) && !$this->usuarioService->changeProfile($ArChangesUsers) ) {
            return new Retorno('Ocorreu um erro ao realizar as trocas de Perfil os Usuários.');
        }
        $BoReturn = $this->perfilDao->delete($IDPerfil);
        if( $IDPerfil == Dracosys::getLoggedUser()->getTPerfil()->getIDPerfil() ) {
            $this->usuarioService->reload();
        }
        return $BoReturn ? new Retorno('Perfil deletado com sucesso.', 1) : new Retorno('Ocorreu um erro na hora de deletar o Perfil.') ;
    }

    private function fillProfile(&$MxProfile) {
        if( !empty($MxProfile) ) {
            $this->fillProfileWithPosition($MxProfile);
            $this->fillProfileWithPermissions($MxProfile);
        }
    }

    private function fillProfileWithPermissions($MxProfile) {
        if(!is_array($MxProfile)) {
            $MxProfile->setActions($this->acaoService->findByProfile($MxProfile->getIDPerfil()));
            return;
        }
        foreach($MxProfile as &$Profile) {
            $Profile->setActions( $this->acaoService->findByProfile($Profile->getIDPerfil()) );
        }
    }

}

?>
